package com.neuedu.javase.thread;

/**
 * 项目：      javaadvice
 * 类名:       SellTicket
 * 创建时间：  2020/11/24  10:28
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class SellTicket implements Runnable {
    private String name = "";
    public static int num = 50;
    
    
    private Object obj = new Object();

    public SellTicket(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (num>0) {
            System.out.println(name+"在卖票\t "+getTicket());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //方法级别的同步锁
    private synchronized  int getTicket(){
        return num--;
    }

    //对象级别的锁
//    private  int getTicket(){
//
//        //多个线程可以通知执行
//        synchronized (obj){
//            return num--;
//        }
//    }

    public static void main(String[] args) {


        new Thread(new SellTicket("王小花")).start();
        new Thread(new SellTicket("牛晓磊")).start();
    }

}